package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueListVo;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.domain.vo.TbClueExcelVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {
    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;
    @Autowired
    private TbClueMapper tbClueMapper;

    @Override
    public void add(ClueTrackRecordVo tbClueTrackRecord) {

        TbClue tbClue = new TbClue();
         tbClue.setId(tbClueTrackRecord.getClueId());
        /*if (trackRecord != null) {
            tbClueTrackRecordMapper.update(tbClueTrackRecord);
            tbClueTrackRecordMapper.updateBytbClue(tbClue);
        }*/
        tbClue.setCreateTime(new Date());
        BeanUtils.copyProperties(tbClueTrackRecord, tbClue);

        //tbClue.setCreateBy(SecurityUtils.getUsername());
        String username = SecurityUtils.getUsername();
        tbClueTrackRecordMapper.update(tbClue);
        tbClueTrackRecordMapper.insert(tbClueTrackRecord, username);
        tbClue.setFalseCount(tbClue.getFalseCount() + 1);

       /* if (tbClue.getStatus().equals(TbClue.StatusType.UNFOLLOWED)) {
            tbClueTrackRecordMapper.insert(tbClueTrackRecord);
            tbClueTrackRecordMapper.insertOther(tbClue);
            tbClue.setStatus(TbClue.StatusType.FOLLOWING.toString());
            tbClue.setUpdateTime(new Date());
        }
        if (tbClue.getStatus().equals(TbClue.StatusType.FOLLOWING)){
            tbClueTrackRecordMapper.insert(tbClueTrackRecord);
            tbClue.setCreateTime(new Date());
            tbClueTrackRecordMapper.update(tbClue);
            tbClue.setFalseCount(tbClue.getFalseCount()+1);
            tbClueTrackRecordMapper.insert(tbClueTrackRecord);
        }*/


    }

    @Override
    public TableDataInfo list(Long clueId) {
        ClueListVo clueListVo = new ClueListVo();
        List<ClueListVo> list =tbClueTrackRecordMapper.list(clueId);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(list.size());
        tableDataInfo.setRows(list);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setParams(null);

        return tableDataInfo;
    }
}
